package server.business.play.handler;

import server.ServerHandler;
import server.ServerTransportProxy;
import server.business.poker.util.PokerCheck;
import server.commonData.PokerPlayer;
import server.commonData.Table;
import server.commonData.User;
import server.messageId.S2CMessageId;
import server.msgBean.PlayMessage.ChoosePokerReq;
import server.msgBean.PlayMessage.ChoosePokerRes;

import com.core.initialize.Message;
import com.google.protobuf.GeneratedMessage;
/**
 * 选牌
 * @author King
 *
 */
public class PlayXuanPaiHandler extends ServerHandler {

	@Override
	public Class<? extends GeneratedMessage> initBodyClass() {
		return ChoosePokerReq.class;
	}

	@Override
	protected void handler(Message msg, User user) throws Throwable
	{
		ChoosePokerReq req = msg.getBody();
		Table table  = user.getTable();
		PokerPlayer pokerPlayer = table.getPlayingPlayers().get(user.getId());
		if(pokerPlayer.getPokerData().isChoose())
		{
			System.err.println("已经选牌了的");
			return;
		}
		if(pokerPlayer.getPokerData().getOwnPoker()==null)
		{
			System.err.println("还没有获得牌呢");
			return;
		}
		if(req.hasHasNiu())
		{
			pokerPlayer.getPokerData().setPokerType(PokerCheck.getNiuType(pokerPlayer.getPokerData().getOwnPoker()));
		}
		else
		{
			pokerPlayer.getPokerData().setPokerType(PokerCheck.MEINIU);
		}
		msg.clearDate();
		msg.setModuleAction(S2CMessageId.PLAY.MODULE_ID, S2CMessageId.PLAY.XUANPAI_RES);
		ChoosePokerRes.Builder builder = ChoosePokerRes.newBuilder();
		builder.setUserId(user.getId());
		builder.setPokerType(pokerPlayer.getPokerData().getPokerType());
		for(int ownPoker:pokerPlayer.getPokerData().getOwnPoker())
		{
			builder.addPokerNum(ownPoker);
		}
		msg.setBody(builder);
		ServerTransportProxy.sendMsgByUser(msg, table.getUsers());
	}
}
